package org.dtrd.modules.doctor.controller;


import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.doctor.entity.bean.UserBankDetail;
import org.dtrd.modules.doctor.entity.bean.UserBankListInfo;
import org.dtrd.modules.doctor.entity.request.UserBankListRequest;
import org.dtrd.modules.doctor.service.IDtrdEntRdUserBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author QR
 * @since 2021-12-14
 */
@Api(tags = "医生-钱包  ")
@RestController
@RequestMapping("/doctor/bank/")
public class DoctorBankController extends BaseController {

    @Autowired
    private IDtrdEntRdUserBankService userBankService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "获取用户的默认银行卡信息", response = UserBankDetail.class)
    @GetMapping("get")
    public Result<?> getUserBank(@RequestParam Integer doctorId){
        UserBankDetail info = userBankService.getUserDefaultBank(doctorId);
        String logContent = String.format("获取用户的默认银行卡信息，查询id：%d", doctorId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询用户银行信息列表", response = UserBankListInfo.class)
    @PostMapping("list")
    @AutoLog(value="查询用户银行信息列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getUserBankList(@RequestBody UserBankListRequest request){
        List<UserBankListInfo> list = userBankService.getUserBankList(request);
        String logContent = String.format("查询用户银行信息列表, 查询条件: %s", request.toString());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }

    @ApiOperation("新增或修改用户银行信息")
    @PostMapping("edit")
    public Result<?> saveOrUpdateUserBank(@RequestBody UserBankDetail dto){
        // 校验参数: userId userName bankCard bankId
        if (isIntParamInvalid(dto.getUserBankUserId())
                || dto.getUserBankBankId() == null
                || StrUtil.isBlank(dto.getUserBankBankCard())
                || StrUtil.isBlank(dto.getUserBankUserName())) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = userBankService.saveOrUpdateUserBank(dto);
        String logContent = String.format("新增或修改用户银行信息, 用户id: %d", dto.getUserBankUserId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除用户银行信息")
    @DeleteMapping("remove")
    @AutoLog(value="删除用户银行信息", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeUserBank(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = userBankService.removeUserBank(id);
        String logContent = String.format("删除, 删除id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
